package cn.iocoder.yudao.module.system.util;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

/**
 * @author : hepburn
 * @description : 生成随机串
 */
public class RandomUtil {
    private static Random random = new Random();
    private static String randomString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yy-MM-dd");
    private static DateTimeFormatter DATE_FORMATTER2 = DateTimeFormatter.ofPattern("HH:mm:ss");
    /**
     * java生成随机数字和字母组合10位数
     * @return
     */
    public static String getRandomNickname() {
        String val = "";
        Random random = new Random();
        for (int i = 0; i < 8; i++) {
            // 输出字母还是数字
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            // 字符串
            if ("char".equalsIgnoreCase(charOrNum)) {
                // 取得大写字母还是小写字母
                int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (choice + random.nextInt(26));
            } else if ("num".equalsIgnoreCase(charOrNum)) { // 数字
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }

    /**
     * 获取随机创惠码流水号
     * @return 创惠码流水号
     */
    public static String getRandomChuanghuiNo(Integer userId) {
        long timestamp = System.currentTimeMillis();
        String src = ""+ timestamp;
        String randomStr = src.substring(src.length()-5);
        LocalDateTime curTime = LocalDateTime.now();
        String timeStr = curTime.format(DATE_FORMATTER).replace("-", "");
        String userText = String.format("%08d", (userId == null)?0:userId);
        return timeStr+userText+randomStr;
    }

    /**
     * 获取随机订单号
     * @return 订单号
     */
    public static String getRandomOrderSn(Integer userId) {
        long timestamp = System.currentTimeMillis();
        String src = ""+ timestamp;
        String randomStr = src.substring(src.length()-1);
        LocalDateTime curTime = LocalDateTime.now();
        String timeStr = curTime.format(DATE_FORMATTER).replace("-", "");
        String timeStr2 = curTime.format(DATE_FORMATTER2).replace(":", "");
        String userText = String.format("%08d", (userId == null)?0:userId);
        return timeStr+userText+timeStr2+randomStr;
    }

    /**
     * 获取随机图片文件名
     * @return 文件名
     */
    public static String getRandomImageFileName() {
        long timestamp = System.currentTimeMillis();
        return "QRC"+timestamp+".png";
    }
    /**
     * 获取随机图片文件名
     * @return 文件名
     */
    public static String getRandomImageFileName2() {
        long timestamp = System.currentTimeMillis();
        return "Poster"+timestamp+".png";
    }

    /**
     * 获取随机创惠码
     * @return
     */
    public static String getRandomChuanghuiCode() {
        return String.valueOf(randomString.charAt(random.nextInt(32)));
    }


    public static void main(String[] args) {
        System.out.println(getRandomChuanghuiNo(16));
    }
}
